from pyspark import SparkConf
from pyspark import SparkContext

if __name__ == '__main__':
    conf = SparkConf().setMaster("local[2]").setAppName("wordcount")
    sc = SparkContext(conf=conf)

    s = sc.accumulator(0) # 创建一个累加器,而且初始值为0
    # 所有单词的总数
    lines = sc.textFile("../data/test1.txt")
    words = lines.flatMap(lambda line: line.split(","))


    def sum_numbers(w):
        print(w)
        s.add(1)
        # print(s.value) # 不能写

    words.foreach(sum_numbers)

    print(s.value) # 把各个分节点中的累加器做更新
